package com.explam.springboot.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.explam.springboot.common.ResultUtil;
import com.explam.springboot.mapper.PropertyMapper;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.explam.springboot.service.IPropertyService;
import com.explam.springboot.entity.Property;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 家庭资产 前端控制器
 * </p>
 *
 * @author 作者
 * @since 2024-03-25
 */
@RestController
@RequestMapping("/admin/property")
public class PropertyController {

    @Resource
    private IPropertyService propertyService;

    @Resource
    private PropertyMapper propertyMapper;

    @GetMapping("/total-money")
    public ResultUtil getPropertyTotal(){
        Double propertyTotal = propertyMapper.getPropertyTotal();
        return ResultUtil.success("总活期余额", propertyTotal);
    }

    //新增和修改
    @PostMapping("/update")
    public ResultUtil update(@RequestBody Property property){
        propertyService.saveOrUpdate(property);
        return ResultUtil.success();
    }

    //新增和修改
    @PostMapping("/save")
    public ResultUtil save(@RequestBody Property property){
        propertyService.save(property);
        return ResultUtil.success();
    }

        //删除
    @GetMapping("/delete/id={id}")
    public ResultUtil delete(@PathVariable Integer id) {
        propertyService.removeById(id);
        return ResultUtil.success();
    }

    //批量删除
    @PostMapping("/del/batch")
    public ResultUtil deleteBatch(@RequestBody List<Integer> ids){
         propertyService.removeByIds(ids);
        return ResultUtil.success();
    }

        //查询所有数据
    @GetMapping("/all")
    public ResultUtil findAll() {
        return ResultUtil.success(propertyService.list());
    }

        //根据id查询
    @GetMapping("/get-property/id={id}")
    public ResultUtil findOne(@PathVariable Integer id) {
        return ResultUtil.success(propertyService.getById(id));
    }

        //分页查询
    @GetMapping("/page")
    public ResultUtil findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String nickname,
                           @RequestParam(defaultValue = "") String propertyName) {
        QueryWrapper<Property> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        if (!"".equals(nickname)){
            queryWrapper.like("nick_name", nickname);
        }
        if (!"".equals(propertyName)){
            queryWrapper.like("property_name", propertyName);
        }
        return ResultUtil.success(propertyService.page(new Page<>(pageNum, pageSize),queryWrapper));
    }
}

